<?php

namespace App\Models\Train\Invite;

use App\Trip\Passengers\Passengers;
use App\Trip\Traits\ModelDateTrait;
use App\Trip\Traits\PassengerAttributeTrait;
use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;

/**
 * App\Models\Train\Invite\InviteDetails
 *
 * @property int         $id
 * @property int         $invite_id      邀请id
 * @property string      $user_id        加入的用户ID
 * @property string      $truename       乘客姓名
 * @property int         $identity_type  证件类型 1 身份证
 * @property string      $identity_code  证件号
 * @property int         $passenger_type 乘客类型 1 成人 2 学生 3 儿童
 * @property string      $mobile         手机号
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @method static Builder|InviteDetails newModelQuery()
 * @method static Builder|InviteDetails newQuery()
 * @method static Builder|InviteDetails query()
 * @method static Builder|InviteDetails whereCreatedAt($value)
 * @method static Builder|InviteDetails whereId($value)
 * @method static Builder|InviteDetails whereIdentityCode($value)
 * @method static Builder|InviteDetails whereIdentityType($value)
 * @method static Builder|InviteDetails whereInviteId($value)
 * @method static Builder|InviteDetails whereMobile($value)
 * @method static Builder|InviteDetails wherePassengerType($value)
 * @method static Builder|InviteDetails whereTruename($value)
 * @method static Builder|InviteDetails whereUpdatedAt($value)
 * @method static Builder|InviteDetails whereUserId($value)
 * @mixin Eloquent
 */
class InviteDetails extends Model
{
    use HasFactory, ModelDateTrait, PassengerAttributeTrait;

    protected $table = 'train_trip_invite_details';

    protected $fillable
        = [
            'invite_id',
            'user_id',
            'truename',
            'identity_type',
            'identity_code',
            'passenger_type',
            'mobile',
        ];

    public function user_id()
    {
        return $this->getAttribute('user_id');
    }

    /**
     * @throws Exception
     */
    public function participate(Invite $invite, $data): self
    {
        $data['invite_id'] = $invite->invite_id;
        $data['truename']  = Passengers::dealTrueName($data);
        $validator         = Validator::make($data,
            [
                'invite_id'      => "required",
                'user_id'        => "required",
                'truename'       => "required",
                'identity_type'  => "required|in:1",
                'identity_code'  => "required|identify_number",
                'passenger_type' => "required|in:1",
                'mobile'         => "required|phone",
            ],
            [
                'identity_code.identify_number' => "身份证格式错误",
                'mobile.phone'                  => "手机号格式错误",
            ]);
        if ($validator->fails()) {
            throw new Exception($validator->errors()->first() ?: "参数错误");
        }

        if ($invite->getUserId() == $data['user_id']) {
            throw new Exception("自己不能加入");
        }

        if ($this->where('identity_code', $data['identity_code'])->where('invite_id', $data['invite_id'])->first()) {
            throw new Exception("该乘客已加入过该邀请");
        }

        if ($join = $this->where('user_id', $data['user_id'])->where('invite_id', $data['invite_id'])->first()) {
            return $join;
        }

        return self::create($data);
    }


}
